package cn.rick.controller;

import cn.dev33.satoken.util.SaResult;
import cn.rick.entity.SysDictData;
import cn.rick.entity.SysDictType;
import cn.rick.service.ISysDictDataService;
import cn.rick.service.ISysDictTypeService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * @author Rick
 * @date 2022-10-20 16:09
 */
@RestController
@RequestMapping("/api/dictType")
@Api(value = "字典类型控制", tags = {"字典类型控制"})
public class DictTypeController {

    @Resource
    private ISysDictTypeService iSysDictTypeService;

    @Resource
    private ISysDictDataService iSysDictDataService;

    @GetMapping("/getAll")
    @ApiOperation("获取所有字典类型")
    public SaResult getAll() {
        LambdaQueryWrapper<SysDictType> query = new LambdaQueryWrapper<>();
        query.orderByDesc(SysDictType::getCreateTime);
        List<SysDictType> list = iSysDictTypeService.list(query);
        return SaResult.data(list);
    }

    /**
     * 通过参数查询所有字典类型
     *
     * @param sysDictType 实体类
     * @param pageNum     当前页
     * @param pageSize    页大小
     * @param startDate   创建时间开始时间
     * @param endDate     创建时间结束时间
     * @return
     */
    @GetMapping("/getData")
    @ApiOperation("通过参数查询所有字典类型")
    public SaResult selectAllByParam(SysDictType sysDictType, Integer pageNum, Integer pageSize, String startDate, String endDate) {
        PageHelper.startPage(pageNum, pageSize);
        List<SysDictType> sysDictTypes = iSysDictTypeService.selectAllByParam(sysDictType, startDate, endDate);
        PageInfo<SysDictType> pageInfo = new PageInfo<>(sysDictTypes);
        return SaResult.data(pageInfo);
    }

    /**
     * 新增、修改字典类型
     *
     * @param sysDictType
     * @return
     */
    @PostMapping("/save")
    @ApiOperation("新增、修改字典类型")
    public SaResult save(@RequestBody SysDictType sysDictType) {
        if (Objects.nonNull(sysDictType)) {
            if (sysDictType.getDictId() != null) {
                SysDictType byId = iSysDictTypeService.getById(sysDictType.getDictId());
                LambdaQueryWrapper<SysDictData> queryWrapper = new LambdaQueryWrapper<>();
                queryWrapper.eq(SysDictData::getDictType, byId.getDictType());
                SysDictData sysDictData = new SysDictData();
                sysDictData.setDictType(sysDictType.getDictType());
                iSysDictDataService.update(sysDictData, queryWrapper);
                boolean save = iSysDictTypeService.updateById(sysDictType);
                if (save) {
                    return SaResult.ok("修改成功");
                } else {
                    return SaResult.error("修改失败，请稍后再试");
                }
            } else {
                LambdaQueryWrapper<SysDictType> queryWrapper1 = new LambdaQueryWrapper<>();
                queryWrapper1.eq(SysDictType::getDictType, sysDictType.getDictType());
                SysDictType one = iSysDictTypeService.getOne(queryWrapper1);
                if (Objects.nonNull(one)) {
                    return SaResult.error("字典类型已存在");
                } else {
                    boolean save = iSysDictTypeService.save(sysDictType);
                    if (save) {
                        return SaResult.ok("新增成功");
                    } else {
                        return SaResult.error("修改失败，请稍后再试");
                    }
                }
            }
        } else {
            return SaResult.error("参数错误！请稍后再试");
        }
    }

    /**
     * 删除字典类型
     *
     * @param dictId id
     * @return
     */
    @DeleteMapping("/del")
    @ApiOperation("删除字典类型")
    public SaResult del(Long dictId) {
        SysDictType byId = iSysDictTypeService.getById(dictId);
        LambdaQueryWrapper<SysDictData> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysDictData::getDictType, byId.getDictType());
        iSysDictDataService.remove(queryWrapper);
        if (iSysDictTypeService.removeById(dictId)) {
            return SaResult.ok("删除成功");
        } else {
            return SaResult.error("删除失败，请稍后再试");
        }
    }
}
